{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "添加外部引用\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "#load \"..\\Extension\\LoadBaseTool.csx\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组切片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从起始索引读取到末尾\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2, 3, 4, 5\n",
      "2, 3, 4, 5\n"
     ]
    }
   ],
   "source": [
    "byte[] _array = [1, 2, 3, 4, 5];\n",
    "\n",
    "byte[] _sub1_1 = _array[1.._array.Length]; // 左闭右开\n",
    "_sub1_1.Dump();\n",
    "\n",
    "byte[] _sub1_2 = _array[1..];\n",
    "_sub1_2.Dump();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对切片数组的修改不会改变原数组\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1, 2, 3, 4, 5\n",
      "21, 3, 4, 5\n",
      "22, 3, 4, 5\n"
     ]
    }
   ],
   "source": [
    "_sub1_1[0] = 21;\n",
    "_sub1_2[0] = 22;\n",
    "_array.Dump();\n",
    "_sub1_1.Dump();\n",
    "_sub1_2.Dump();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从起始索引读取固定长度\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1, 2\r\n"
     ]
    }
   ],
   "source": [
    "int _start = 0;\n",
    "int _length = 2;\n",
    "byte[] _sub2 = _array[_start..(_start + _length)];\n",
    "_sub2.Dump();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "倒序取元素\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "3\n",
      "3, 4, 5\n",
      "4, 5\n",
      "3, 4\n"
     ]
    }
   ],
   "source": [
    "byte _byte1 = _array[^1];\n",
    "_byte1.Dump();\n",
    "\n",
    "byte _byte2 = _array[^3];\n",
    "_byte2.Dump();\n",
    "\n",
    "// 取最后的三个（从倒数第三个到最后），这里的倒数第三个，其实就是正数第二个，相当于_array[2..]\n",
    "byte[] _sub3 = _array[^3..];\n",
    "byte[] _sub4 = _array[3..];\n",
    "_sub3.Dump();\n",
    "_sub4.Dump();\n",
    "\n",
    "byte[] _sub5 = _array[^3..^1];\n",
    "_sub5.Dump();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1, 2, 3, 4\r\n"
     ]
    }
   ],
   "source": [
    "byte[] _array = [1,2,3,4];\n",
    "_array.Dump();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果其中有一个数组是 null，可以用下面的方法来实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1, 2, 3, 4\r\n"
     ]
    }
   ],
   "source": [
    "byte[] _array_2 = null;\n",
    "byte[] _array_1 = [.._array, .._array_2 ?? []];\n",
    "_array_1.Dump();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组类型的转换\n",
    "\n",
    "主要就是使用`Array.ConverterAll()`方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1, 2, 3\r\n"
     ]
    }
   ],
   "source": [
    "string[] _string_array = [\"1\", \"2\", \"3\"];\n",
    "int[] _int_array = Array.ConvertAll<string, int>(_string_array, _item => int.Parse(_item));\n",
    "\n",
    "_int_array.Dump();"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "csharp"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
